//
//  GRShowMoreListVC.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/10.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRShowMoreListVC.h"
#import "GRStoreGoodTableViewCell.h"
#import "GRSotreCommentTableViewCell.h"
#import "GRStoreMsgTableViewCell.h"
#import "GRStoreCommentModel.h"
#import "GRDiscountGoodModel.h"
#import "GRNewsModel.h"
#import "GRSubmitOrderVC.h"
#import "GRNewsDetailViewController.h"
#import "GRGoodDetailPageVC.h"
@interface GRShowMoreListVC ()<GRStoreGoodTableViewCellDelegate>
{
    NSInteger currentPage;
}
@property(nonatomic,strong)NSMutableArray * dataSourceArr;
@end

@implementation GRShowMoreListVC
-(NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr =  [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerNib:[UINib nibWithNibName:@"GRStoreGoodTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GRStoreGoodTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"GRStoreMsgTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GRStoreMsgTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"GRSotreCommentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GRSotreCommentTableViewCell"];
    WEAKSELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1 ;
        [weakSelf requestListWithfreshType:GRRefreshForHeader];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        currentPage++;
        [weakSelf requestListWithfreshType:GRRefreshForFooter];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)requestListWithfreshType:(GRRefresh)freshType{
    NSDictionary * dic ;
    NSString * path;
    if (_listType == ShowMoreListTypeDiscountGood) {
        self.navigationItem.title = @"优惠套餐";
        dic = @{
                @"pageNum": @(currentPage),
                @"pageSize": @(10)
                };
        path = [NSString stringWithFormat:@"%@onePay/discountList/%ld",GRDeliveryAPI,self.listId];
    }else if (_listType == ShowMoreListTypeComment){
         self.navigationItem.title = @"全部评价";
        dic = @{
                @"id":@(self.listId),
                @"pageNum":@(currentPage),
                @"pageSize": @(10),
                @"type":_isStore? @"store":@"discount"
                };
        
        path = [NSString stringWithFormat:@"%@onePay/commentList",GRDeliveryAPI];
    }else{
         self.navigationItem.title = @"热门资讯";
        dic = @{
                @"pageNum": @(currentPage),
                @"pageSize": @(10)
                };
        path = [NSString stringWithFormat:@"%@onePay/newsList/%ld",GRDeliveryAPI,self.listId];
    }
    [RequestManager TouristsRequestWithURLString:path Method:GR_POST Paramers:dic completionHandler:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            NSLog(@"%@",responseObject);
            if (freshType == GRRefreshForHeader) {
                [self.dataSourceArr removeAllObjects];
            }
                NSArray * dataList = responseObject[@"dataList"];
                if (Array(dataList)) {
                    for (NSDictionary *goodDic in dataList) {
                        if (_listType == ShowMoreListTypeDiscountGood) {
                            GRDiscountGoodModel * goodModel = [GRDiscountGoodModel yy_modelWithDictionary:goodDic];
                            [self.dataSourceArr addObject:goodModel];
                        }else if(_listType ==ShowMoreListTypeComment ){
                            GRStoreCommentModel *commentModel = [GRStoreCommentModel yy_modelWithDictionary:goodDic];
                            [self.dataSourceArr addObject:commentModel];
                        }else{
                            GRNewsModel * newsModel = [GRNewsModel yy_modelWithDictionary:goodDic];
                            [self.dataSourceArr addObject:newsModel];
                        }
                    }
                }
                    [self.tableView reloadData];
            }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.dataSourceArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_listType == ShowMoreListTypeComment) {
        GRStoreCommentModel * commentModel = self.dataSourceArr[indexPath.row];
        return [commentModel cellHeight];
    }else if (_listType == ShowMoreListTypeDiscountGood){
        GRDiscountGoodModel * model = self.dataSourceArr[indexPath.row];
        return [model cellHeight];
    }else{
        GRNewsModel * newsModel = self.dataSourceArr[indexPath.row];
        return [newsModel cellHeight];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_listType == ShowMoreListTypeDiscountGood) {
        GRStoreGoodTableViewCell * goodCell = [tableView dequeueReusableCellWithIdentifier:@"GRStoreGoodTableViewCell" forIndexPath:indexPath];
        goodCell.delegate = self;
        goodCell.discountGoodModel = self.dataSourceArr[indexPath.row];
        return goodCell;
    }else if (_listType == ShowMoreListTypeComment){
        GRSotreCommentTableViewCell * commentCell = [tableView dequeueReusableCellWithIdentifier:@"GRSotreCommentTableViewCell" forIndexPath:indexPath];
        commentCell.commentModel = self.dataSourceArr[indexPath.row];
        return commentCell;
        
    }else{
        GRStoreMsgTableViewCell * newsCell = [tableView dequeueReusableCellWithIdentifier:@"GRStoreMsgTableViewCell" forIndexPath:indexPath];
        newsCell.newsModel = self.dataSourceArr[indexPath.row];
        return newsCell;
        
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_listType == ShowMoreListTypeDiscountGood) {
        GRGoodDetailPageVC *goodDetailVC = [[GRGoodDetailPageVC alloc]init];
        GRDiscountGoodModel * goodModel = self.dataSourceArr[indexPath.row];
        goodDetailVC.goodId = goodModel.id;
        [self.navigationController pushViewController:goodDetailVC animated:YES];
    }else  if (_listType == ShowMoreListTypeNews){
        GRNewsDetailViewController *newVC = [[GRNewsDetailViewController alloc]init];
        GRNewsModel * newsModle = self.dataSourceArr[indexPath.row];
        newVC.newsId = newsModle.id;
        [self.navigationController pushViewController:newVC animated:YES];
    }
}

-(void)purchaseAction:(NSInteger)goodId{
    GRSubmitOrderVC * submitOrderId = [[GRSubmitOrderVC alloc]init];
    submitOrderId.goodId = goodId;
    [self.navigationController pushViewController:submitOrderId animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
